package com.dyxy.tms.module;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dyxy.tms.ADInfo;
import com.dyxy.tms.App;
import com.dyxy.tms.AppConfig;
import com.dyxy.tms.R;
import com.dyxy.tms.UserInfo;
import com.dyxy.tms.model.GetData;
import com.dyxy.tms.model.IconBaseAdapter;
import com.dyxy.tms.model.MyAdapter;
import com.dyxy.tms.utils.OkHttpUtils;
import com.dyxy.tms.views.ImageCycleView;
import com.dyxy.tms.views.ImageCycleView.ImageCycleViewListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 
 * 首页
 * 
 * @author zk && wjx
 *
 */
public class IndexFragment extends Fragment {
	private ImageCycleView mAdView;
	private String indexurl = AppConfig.url + "/TMS/index.php";
	private ArrayList<ADInfo> infos = new ArrayList<ADInfo>();
	private String result;
	private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

		@Override
		public void onImageClick(ADInfo info, int position, View imageView) {
		}

		@Override
		public void displayImage(String imageURL, ImageView imageView) {
			ImageLoader.getInstance().displayImage(imageURL, imageView);// 使用ImageLoader对图片进行加装！
		}
	};
	private GridView gridview;
	private ListView listview;
	private List<Map<String, Object>> list, list2;
	private OkHttpUtils ohu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	private ImageCycleView findViewById(int tabIndexImgcycleview) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.tab_index, container, false);
		init(v);
		setIcon();
		setlb();
		setlv();
		return v;
	}

	private void init(View v) {
		mAdView = (ImageCycleView) v.findViewById(R.id.tab_index_ImgCycleView);
		gridview = (GridView) v.findViewById(R.id.tab_index_gdV);
		listview = (ListView) v.findViewById(R.id.tab_index_lv);
		try {
			result = OkHttpUtils.getInstance().get(indexurl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		OkHttpUtils.getInstance().get(indexurl);
//		result = App.getInstance().okHttpUtils.getresult();
	}

	private void setIcon() {
		if (result == null) {
//			result = ohu.getresult();
//			result = OkHttpUtils.getInstance().getresult();
			try {
				result = OkHttpUtils.getInstance().get(indexurl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (result != null) {
			list = UserInfo.getIndexIconList(result);
			if (list != null) {
				IconBaseAdapter adapter = new IconBaseAdapter(getActivity(), list);
				gridview.setAdapter(adapter);
			}
		}
	}

	private void setlb() {
		if (result == null) {
//			result = ohu.getresult();
//			result = OkHttpUtils.getInstance().getresult();
			try {
				result = OkHttpUtils.getInstance().get(indexurl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (result != null) {
			list = UserInfo.getIndexLBList(result);
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				ADInfo info = new ADInfo();
				info.setUrl(map.get("img").toString());
				infos.add(info);
			}
			mAdView.setImageResources(infos, mAdCycleViewListener);
		}

	}

	private void setlv() {
		// TODO Auto-generated method stub
		if (result == null) {
//			result = ohu.getresult();
//			result = OkHttpUtils.getInstance().getresult();
			try {
				result = OkHttpUtils.getInstance().get(indexurl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (result != null) {
			list2 = UserInfo.getIndexCourseList(result);
			if (list2 != null) {

				listview.setAdapter(new MyAdapter().courseSimpleAdapter(getActivity(), list2,
						new String[] { "adress", "teacher", "name" }, new int[] { R.id.item_index_lv_tv_coursetime,
								R.id.item_index_lv_tv_teachername, R.id.item_index_lv_tv_coursename }));
			}
		}
	}
}
